package com.example.mytestdemo.text

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityTextScrollLayoutBinding

/**
 *
 * 文本从下往上滚
 */
class TextScrollActivity : BaseViewBindActivity<ActivityTextScrollLayoutBinding>() {
    override fun getLayoutInflateId(): Int {
        return R.layout.activity_text_scroll_layout
    }

    override fun initData() {

        val containerMinutes = findViewById<LinearLayout>(R.id.containerMinutes)
        val containerSeconds = findViewById<LinearLayout>(R.id.containerSeconds)

        // 创建两个TextView用于显示分钟和秒钟
        val minutesTextView1 = createDigitTextView()
        val minutesTextView2 = createDigitTextView()
        val secondsTextView1 = createDigitTextView()
        val secondsTextView2 = createDigitTextView()

        // 添加TextView到布局
        containerMinutes.addView(minutesTextView1)
        containerMinutes.addView(minutesTextView2)
        containerSeconds.addView(secondsTextView1)
        containerSeconds.addView(secondsTextView2)

        // 设置初始时间
        minutesTextView1.text = "0"
        minutesTextView2.text = "0"
        secondsTextView1.text = "0"
        secondsTextView2.text = "0"

        // 创建ValueAnimator来控制时间的变化
        val valueAnimator = ValueAnimator.ofFloat(0f, 1f)
        valueAnimator.duration = 60000 // 60秒，可以根据需要调整

        // 设置时间变化监听器
        valueAnimator.addUpdateListener { animator ->
            val fraction = animator.animatedValue as Float

            // 计算分钟和秒钟的位移
            val translationYMinutes1 = -fraction * minutesTextView1.height
            val translationYMinutes2 = -fraction * minutesTextView2.height
            val translationYSeconds1 = -fraction * secondsTextView1.height
            val translationYSeconds2 = -fraction * secondsTextView2.height

            // 更新TextView的位置
            minutesTextView1.translationY = translationYMinutes1
            minutesTextView2.translationY = translationYMinutes2
            secondsTextView1.translationY = translationYSeconds1
            secondsTextView2.translationY = translationYSeconds2
        }

        // 启动动画
        valueAnimator.start()




        //动画
        setIdentifyNum(234589567)

    }

    private fun setIdentifyNum(identifyNum: Long) {
        if (mBind.rollText.isRunning()) {
            return
        }
        mBind.rollText.setText(identifyNum)
        mBind.rollText.startAnimation()
    }

    private fun createDigitTextView(): TextView {
        val textView = TextView(this)
        val layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        textView.layoutParams = layoutParams
        textView.textSize = 24f
        textView.setTextColor(resources.getColor(R.color.black))
        textView.gravity = Gravity.CENTER
        return textView
    }

    override fun initUI() {
    }

}